import { defineStore } from 'pinia'

interface State {
  forcedLogin: boolean;
  hasLogin: boolean;
  update: boolean;
  bNeedRefreshHome: boolean;
  loginInfo: Record<string, any>;
  enjoyServiceIp: Record<string, any>;
  limitCode: Record<string, boolean>;
  address: Array<{
    buyerName: string,
    buyerPhone: string,
    area: string,
    location: string,
    isDefault: boolean
  }>;
}

export const useMainStore = defineStore('main', {
  state: (): State => ({
    forcedLogin: true, // 是否需要强制登录
    hasLogin: false,
    update: false,
    bNeedRefreshHome: false, // 是否需要刷新首页内容展示
    loginInfo: {}, // 登陆对象
    enjoyServiceIp: {}, // 地址管理
    limitCode: {}, // 当前打开模块的权限
    address: [
      { "buyerName": "瑟瑟先生", "buyerPhone": "15988986988", "area": "广东省 深圳市 南山区", "location": "深南大道 高新科技园5F-24B", "isDefault": true },
      { "buyerName": "包子", "buyerPhone": "19988562232", "area": "湖北省 武汉市 洪山区", "location": "湖北省武汉市洪山区珞珈山街道八一路299号 武汉大学宏博学生公寓16栋2-203", "isDefault": false },
      { "buyerName": "七七", "buyerPhone": "17899654412", "area": "湖北省 武汉市 洪山区", "location": "湖北省武汉市洪山区珞珈山街道八一路299号 华中师范大学博雅学生公寓22栋3-603", "isDefault": false }
    ]
  }),
  actions: {
    login() {
      this.hasLogin = true;
    },
    logout() {
      this.hasLogin = false;
      this.loginInfo = {};
      uni.removeStorageSync('tabBarData');
    },
    loginInfoin(loginInfo: any) {
      this.hasLogin = true;
      this.loginInfo = {
        ...loginInfo,
        sTenantId: loginInfo && loginInfo.idsToken && loginInfo.idsToken.tenantid ? loginInfo.idsToken.tenantid : '',
        access_token: loginInfo && loginInfo.idsToken && loginInfo.idsToken.access_token ? loginInfo.idsToken.access_token : '',
        OARefreshToken: loginInfo && loginInfo.idsToken && loginInfo.idsToken.refresh_token ? loginInfo.idsToken.refresh_token : '',
        tokenExpiresDt: loginInfo && loginInfo.idsToken && loginInfo.idsToken.expires_dt ? loginInfo.idsToken.expires_dt : '',
      };
    },
    setWorkStatus(workStatus: string) {
      this.loginInfo = {
        ...this.loginInfo,
        workStatus
      }
    },
    setUserDefaultMerchantInfo(sDefaultMerchantInfo?:any) {
      this.loginInfo = {
        ...this.loginInfo,
        sDefaultMerchantCode: sDefaultMerchantInfo?.merchantCode,
        sDefaultMerchantName: sDefaultMerchantInfo?.merchantName,
      }
    },
    setUserCurMerchantInfo(merchantInfo?: any) {
      this.loginInfo = {
        ...this.loginInfo,
        ...merchantInfo,
      }
    },
    setOnlineTime(onlineTime: string) {
      this.loginInfo = {
        ...this.loginInfo,
        onlineTime
      }
    },
    enjoyServiceIpin(enjoyServiceIp: any) {
      this.enjoyServiceIp = enjoyServiceIp;
    },
    enjoyServiceIpout() {
      this.enjoyServiceIp = {}
    },
    limitCodeIn(Code:string) {
      const modLimitCode = (data:any) => {
        let list = data.split(''),
          obj:any = {};
        if (list && list.length > 0) {
          for (var i = 0; i < list.length; i++) {
            obj[list[i]] = true;
          }
        }
        return obj;
      };
      this.limitCode = modLimitCode(Code);
    },
    switchStore(sUserStoreHier: string, sUserStore: string, sUserStoreName: string) {
      this.loginInfo = {
        ...this.loginInfo,
        sDefaultStoreHier: sUserStoreHier,
        sDefaultStore: sUserStore,
        sDefaultStoreName: sUserStoreName
      };
    }
  },
  getters: {
    // ... your getters
  },
})
